package com.erp.module.yiJianXiang.sSYiJianXiang

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import com.erp.activity.BaseActivity
import com.erp.myAdapter.ArrayAdapter
import com.erp.bean.TouSuSouSuoBean
import com.erp.Interface.OnUserDataInterface
import com.erp.R
import com.erp.activity.MainKActivity
import com.erp.util.Dialogutil
import com.erp.util.Httputil
import com.erp.util.JsonUtil
import com.erp.util.Myutil
import kotlinx.android.synthetic.main.activity_tousu_sousuo.*
import kotlinx.android.synthetic.main.header_knoya.*
import org.json.JSONException
import org.json.JSONObject
import java.util.*

/**
 * Created by Administrator on 2018/3/1/001.
 * 作者：刘洋
 */

class YiJianXiangSouSuoActivity : BaseActivity(), View.OnClickListener {
    lateinit var touSuSouSuoBean: TouSuSouSuoBean
    private val a = "TousuSouSuoActivity"
    override fun getLayoutId(): Int {
        activityname = a
        return R.layout.activity_tousu_sousuo
    }

    override fun initCreate() {
        initView()
        initData()
    }

    private fun initView() {
        iv_header_left.visibility = View.VISIBLE
        tv_header_title.visibility = View.VISIBLE
        tv_header_title.text = "投诉建议搜索"
        touSuSouSuoBean = TouSuSouSuoBean()
        val bundle = intent.getParcelableExtra<Bundle>("searchParams")
        touSuSouSuoBean = bundle.getParcelable("searchParams")
        iv_header_left.setOnClickListener(this)
        TvStartTime.setOnClickListener(this)
        TvEndTime.setOnClickListener(this)
        BtQueding.setOnClickListener(this)
        if(MainKActivity.ZhuanMaiDianid != "0"){
            LlGongZuoDi.visibility = View.GONE
            LlChangQU.visibility = View.GONE
            LlBuMen.visibility = View.GONE
        }
        val gongzuodi = ArrayList<String>()
        gongzuodi.add("")
        gongzuodi.add("广州")
        gongzuodi.add("清远")
        val bmadapter = ArrayAdapter(this@YiJianXiangSouSuoActivity, R.layout.lv_sp_tvitem, gongzuodi)
        SpGongZuoDi.adapter = bmadapter
        when(touSuSouSuoBean.gongzuodi){
            "" -> SpGongZuoDi.setSelection(0)
            "广州" -> SpGongZuoDi.setSelection(1)
            "清远" -> SpGongZuoDi.setSelection(2)
        }
    }

    private fun initData() {
        Httputil.tousubiao(this@YiJianXiangSouSuoActivity, object : OnUserDataInterface {
            override fun onUserData(data: String) {
                Log.d("TAAAG", "data=" + Myutil.decodeUnicode(data))
                val changqus = ArrayList<String>()
                val bumens = ArrayList<String>()
                changqus.add("")
                bumens.add("")
                try {
                    val jo = JSONObject(data)
                    val changqujo = JsonUtil.getjovolue(jo, "han_qu")
                    val chuangqukeys = changqujo.keys()
                    while (chuangqukeys.hasNext()) {
                        val key = chuangqukeys.next() as String
                        changqus.add(key)
                    }
                    val bumenjo = JsonUtil.getjovolue(jo, "bu_men")
                    val bumenkeys = bumenjo.keys()
                    while (bumenkeys.hasNext()) {
                        val key = bumenkeys.next() as String
                        bumens.add(key)
                    }
                    val cqadapter = ArrayAdapter(this@YiJianXiangSouSuoActivity, R.layout.lv_sp_tvitem, changqus)
                    SpChangqu.adapter = cqadapter
                    val bmadapter = ArrayAdapter(this@YiJianXiangSouSuoActivity, R.layout.lv_sp_tvitem, bumens)
                    SpBumen.adapter = bmadapter
                    for (i in changqus.indices) {
                        if (touSuSouSuoBean.changqu == changqus[i]) {
                            SpChangqu.setSelection(i)
                        }
                    }
                    for (i in bumens.indices) {
                        if (touSuSouSuoBean.bumen == bumens[i]) {
                            SpBumen.setSelection(i)
                        }
                    }
                } catch (e: JSONException) {
                    e.printStackTrace()
                }

            }

            override fun onNot(throwable: String) {
                Log.d("TAAAG", "throwable=" + Myutil.decodeUnicode(throwable))
            }
        })
        val times = ArrayList<String>()
        times.add("创建时间")
        val tadapter = ArrayAdapter(this@YiJianXiangSouSuoActivity, R.layout.lv_sp_tvitem, times)
        SpShijian.adapter = tadapter
        for (i in times.indices) {
            if (touSuSouSuoBean.shijianleibie == times[i]) {
                SpBumen.setSelection(i)
            }
        }
        EtTousu.setText(touSuSouSuoBean.tousu)
        EtJianyi.setText(touSuSouSuoBean.jianyi)
        TvStartTime.text = touSuSouSuoBean.starttime
        TvEndTime.text = touSuSouSuoBean.endtime
    }


    override fun onClick(v: View) {
        when (v) {
            iv_header_left -> {
                finish()
                overridePendingTransition(0, R.anim.end_activity1)
            }
            TvStartTime -> {
                Myutil.notjianpan(this@YiJianXiangSouSuoActivity, iv_header_left)
                Dialogutil.showBottoPopupWindow(TvStartTime, "选择开始时间", TvStartTime!!.text.toString(), this@YiJianXiangSouSuoActivity)
            }
            TvEndTime -> {
                Myutil.notjianpan(this@YiJianXiangSouSuoActivity, iv_header_left)
                Dialogutil.showBottoPopupWindow(TvEndTime, "选择结束时间", TvEndTime!!.text.toString(), this@YiJianXiangSouSuoActivity)
            }
            BtQueding -> {
                sousuo()
                val intent = Intent()
                val bundle = Bundle()
                bundle.putParcelable("searchParams", touSuSouSuoBean)
                intent.putExtra("searchParams", bundle)
                setResult(Activity.RESULT_OK, intent)
                finish()
                overridePendingTransition(0, R.anim.end_activity1)
            }
        }
    }

    fun sousuo() {
        touSuSouSuoBean.gongzuodi = SpGongZuoDi.selectedItem.toString()
        touSuSouSuoBean.changqu = SpChangqu.selectedItem.toString()
        touSuSouSuoBean.bumen = SpBumen.selectedItem.toString()
        touSuSouSuoBean.shijianleibie = SpShijian.selectedItem.toString()
        touSuSouSuoBean.tousu = EtTousu.text.toString()
        touSuSouSuoBean.jianyi = EtJianyi.text.toString()
        touSuSouSuoBean.starttime = TvStartTime.text.toString()
        touSuSouSuoBean.endtime = TvEndTime.text.toString()

    }
}
